package ltd.simulatoruidemo.activities;

import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.RelativeLayout;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import ltd.simulatoruidemo.R;
import ltd.simulatoruidemo.adapter.MyFragmentStatePagerAdapter;
import ltd.simulatoruidemo.bean.LayoutMSGList;
import ltd.simulatoruidemo.entity.Layout;
import ltd.simulatoruidemo.fragment.LayoutFragment;
import ltd.simulatoruidemo.util.FileUtil;
import ltd.simulatoruidemo.util.StorageUtil;
import ltd.viewpager.JazzyViewPager;
import ltd.viewpager.LtdViewPager;

import static ltd.simulatoruidemo.util.JsonUtil.formJson;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    public static int SCREEN_WIDTH ;
    public static int SCREEN_HEIGHT;

    @Bind(R.id.jazzy_pager)
    public JazzyViewPager mViewPager;
    @Bind(R.id.mTabLayout)
    public TabLayout mTabLayout;
//    @Bind(R.id.main_relativelayout)
//    public RelativeLayout relativeLayout ;
    public List<Layout> navigationList ;
    private List<LayoutFragment> listFragment ;
    /*1、如果key的类型已经确定为int类型，那么使用SparseArray，因为它避免了自动装箱的过程，如果key为long类型，它还提供了一个LongSparseArray来确保key为long类型时的使用
    2、如果key类型为其它的类型，则使用ArrayMap*/
    public SparseArray<List<Layout>> fragmentLayoutSArray;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
//        ltd.viewpager.JazzyViewPager jazzyViewPager = new JazzyViewPager(this);
        Point size = new Point();
        getWindowManager().getDefaultDisplay().getRealSize(size);		//加上虚拟按键位置
        SCREEN_WIDTH = size.x ;
        SCREEN_HEIGHT = size.y ;
        Log.e(TAG,"x: " + SCREEN_WIDTH + "   y: " + SCREEN_HEIGHT) ;
        initData();
        createAllView();
        ShowAllview();

//        FragmentManager fragmentManager = getSupportFragmentManager();            不需要添加，直接在用adpter加入viewpager
//        for (LayoutFragment layoutFragment : listFragment){
//            fragmentManager.beginTransaction().add(R.id.jazzy_pager,layoutFragment).commit();
//        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }
    /**
     * 导航条存入navigationList
     * 页面内容存入fragmentLayoutSArray
     * fragmentLayoutSArray内按键值存入每个页面的List<Layout>，每个List就是一个页面，每个Layout就是一个组件的信息。
     */
    private void initData() {
        String path = StorageUtil.getDownloadPath() + "/layout.txt" ;
//        path = "F:\\layout.txt" ;
        readDataFromJson(path);
        if (fragmentLayoutSArray != null){
            System.out.println(fragmentLayoutSArray.size());
        }
    }
    private void readDataFromJson(String fileName){
        String json ;
        navigationList = new ArrayList<>();
        fragmentLayoutSArray = new SparseArray<>();
        List<Layout> fragmentList = new ArrayList<>() ;
        try{
            json = FileUtil.readStringFromFile(fileName);
            LayoutMSGList layoutMSGList = new LayoutMSGList();
            layoutMSGList = (LayoutMSGList) formJson(json,LayoutMSGList.class) ;
            List<Layout> readList = layoutMSGList.getList();
            Iterator<Layout> it = readList.iterator();
            while (it.hasNext()){
                Layout layout = new Layout();
                layout = it.next();
                if (layout.getMode() == 200){   //导航条
                    navigationList.add(layout);
                }else if (layout.getMode() < 200){  //是页内控件
                    int page = layout.getPage();
                    fragmentList = fragmentLayoutSArray.get(page,new ArrayList<Layout>());
                    fragmentList.add(layout) ;
                    fragmentLayoutSArray.put(page,fragmentList);
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }

    }
    private void createAllView(){
        //设置导航条
        for (int i=0; i<navigationList.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(navigationList.get(i).getName()));
        }
        mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        //        mTabLayout.setSelectedTabIndicatorColor(Color.parseColor("#7CCD7C"));
        mTabLayout.setTabTextColors(Color.GRAY,Color.parseColor("#FF4081"));
        //设置fragment
        listFragment = new ArrayList<>();
        for (int i=0 ; i < fragmentLayoutSArray.size() ; i++){
            listFragment.add(LayoutFragment.newInstance(fragmentLayoutSArray.get(i))) ;
        }
    }
    private void ShowAllview(){
        mViewPager.setTransitionEffect(JazzyViewPager.TransitionEffect.RotateDown);
//        mJazzy.setAdapter(new MainAdapter());
        mViewPager.setPageMargin(30);

        mViewPager.setAdapter(new MyFragmentStatePagerAdapter(getSupportFragmentManager(),listFragment,this.mViewPager));
        mViewPager.addOnPageChangeListener(new LtdTabLayoutOnPageChangeListener(mTabLayout));

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
    private class LtdTabLayoutOnPageChangeListener extends TabLayout.TabLayoutOnPageChangeListener implements LtdViewPager.OnPageChangeListener{
        public LtdTabLayoutOnPageChangeListener(TabLayout tabLayout) {
            super(tabLayout);
        }
    }

    public static void SetReflectMargins(int x,int y,int w,int h,View v)
    {
        RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(SCREEN_WIDTH * w/1920,SCREEN_HEIGHT * h/1080);
        lp.setMargins(SCREEN_WIDTH * x/1920, SCREEN_HEIGHT * y/1080, SCREEN_WIDTH * 30 / 1920, SCREEN_HEIGHT * 30 / 1920);
        v.setLayoutParams(lp);
    }
}
